package com.example.wechatappbackend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.wechatappbackend.VO.OrderVO;
import com.example.wechatappbackend.common.CommonResponse;
import com.example.wechatappbackend.common.JsonMsg;
import com.example.wechatappbackend.entity.Order;
import com.example.wechatappbackend.entity.OrderDish;
import com.example.wechatappbackend.persistence.OrderDishMapper;
import com.example.wechatappbackend.persistence.OrderMapper;
import com.example.wechatappbackend.service.OrderService;
import com.example.wechatappbackend.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDishMapper orderDishMapper;

    @Override
    public JsonMsg getOrdersByOpenId(String openId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", openId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        List<OrderVO> orderVOList = new LinkedList<>();
        for(int i=0;i<orderList.size();i++)
        {
            OrderVO orderVOTemp = Utils.orderToOrderVO(orderList.get(i));
            orderVOList.add(orderVOTemp);
        }
        return new JsonMsg("1","SUCCESS",orderVOList);
//        return CommonResponse.createForSuccess("获取订单成功",orderVOList);
    }

    @Override
    public JsonMsg getOrderByOrderId(String OrderId) {
        QueryWrapper<OrderDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", OrderId);
        List<OrderDish> orderDishList = orderDishMapper.selectList(queryWrapper);
        System.out.println("lengthOfList:"+orderDishList.size());
        return new JsonMsg("0","SUCCESS",orderDishList);
//        return CommonResponse.createForSuccess("获取订单成功",orderDishList);

    }

    @Override
    public CommonResponse<OrderVO> insertOrder(String openId, OrderVO orderVO) {
        Order order = new Order();
        order.setUserID(openId);
        order.setRank(order.getRank());
        order.setPeopleNum(orderVO.getPeopleNum());
        order.setEstimatedTime(orderVO.getEstimatedTime());
        order.setOrderTime(orderVO.getOrderTime());
        order.setPaymentTime(orderVO.getPaymentTime());
        order.setOrderPrice(orderVO.getOrderPrice());
        order.setStatus(orderVO.getStatus());
        order.setRestaurantName(orderVO.getRestaurantName());
        order.setRestaurantAvatar(orderVO.getRestaurantAvatar());

        orderMapper.insert(order);
        String orderId = order.getOrderId();
        orderVO.setOrderId(orderId);


//        for(int i = 0;i < orderVO.getOrderDishList().size();i++){
//            OrderDish orderDish = new OrderDish();
//            orderDish.setOrderId(orderId);
//            orderDish.setDishAvatar(orderVO.getOrderDishList().get(i).getDishAvatar());
//            orderDish.setDishName(orderVO.getOrderDishList().get(i).getDishName());
//            orderDish.setDishPrice(orderVO.getOrderDishList().get(i).getDishPrice());
//            orderDish.setDishNum(orderVO.getOrderDishList().get(i).getDishNum());
//            orderDish.setUserComment(orderVO.getOrderDishList().get(i).getUserComment());
//            orderDishMapper.insert(orderDish);
//
//            orderVO.getOrderDishList().get(i).setOrderId(orderId);
//        }

        return CommonResponse.createForSuccess("生成订单成功",orderVO);
    }

    @Override
    public CommonResponse cancleOrder(int orderId) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId).eq("status",0);

        Order order = orderMapper.selectOne(queryWrapper);

        if(order != null){
            order.setStatus(2);//2表示取消的订单（暂时定为2
            UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", orderId);
            orderMapper.update(order,updateWrapper);

            return CommonResponse.createForSuccess("取消订单成功");
        }
        return CommonResponse.createForError("商家正在备餐，取消订单失败");
    }
}
